﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Office.Server.Search.Query;
namespace SharePointSearchBench.Library
{
    /// <summary>
    /// Encapsulates data about managed properties that can be
    /// crawled and specified in searches.
    /// </summary>
    public class SearchProperty
    {
        private string name;
        private string description;
        private string typeName;
        private bool retrievable;
        private bool fullTextQueriable;

        /// <summary>
        /// Constructs the object from a <see cref="System.Data.DataRow"/>.  This
        /// overload is used when constructing the object from a web service call result.
        /// </summary>
        /// <param name="row"></param>
        public SearchProperty(DataRow row)
        {
            this.name = (string)row["Name"];
            this.description = (string)row["Description"];
            this.typeName = (string)row["Type"];
            this.retrievable = (bool)row["Retrievable"];
            this.fullTextQueriable = (bool)row["FullTextQueriable"];
        }

        /// <summary>
        /// Constructs the object from a <see cref="Microsoft.Office.Server.Search.Query.PropertyInformation"/>
        /// object.  This overload is used when constructing the object from 
        /// an object model call result.
        /// </summary>
        /// <param name="propertyInfo"></param>
        public SearchProperty(PropertyInformation propertyInfo)
        {
            this.name = propertyInfo.Name;
            this.description = propertyInfo.Description;
            this.typeName = propertyInfo.Type.FullName;
            this.retrievable = propertyInfo.Retrievable;
            this.fullTextQueriable = propertyInfo.FullTextQueriable;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name
        {
            get { return this.name; }
        }

        /// <summary>
        /// Gets the search property description.
        /// </summary>
        public string Description
        {
            get { return this.description; }
        }

        /// <summary>
        /// Gets the full name of the type.
        /// </summary>
        public string TypeName
        {
            get { return this.typeName; }
        }

        /// <summary>
        /// Gets whether or not this property is
        /// retrievable.
        /// </summary>
        public bool Retrievable
        {
            get { return this.retrievable; }
        }

        /// <summary>
        /// Gets whether or not this property
        /// can be specified in full text searches.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Queriable")]
        public bool FullTextQueriable
        {
            get { return this.fullTextQueriable; }
        }

        /// <summary>
        /// Extracts a collection of <see cref="SearchProperty"/> objects from 
        /// a <see cref="System.Data.DataSet"/>.  
        /// </summary>
        /// <param name="metadata"></param>
        /// <returns></returns>
        /// <remarks>This method assumes a certain DataSet schema is used such as 
        /// one that the SharePoint Search service returns.</remarks>
        public static Collection<SearchProperty> GetPropertiesFromRawDataSet(DataSet metadata)
        {
            Collection<SearchProperty> items = new Collection<SearchProperty>();
            foreach (DataRow row in metadata.Tables[0].Rows)
            {
                items.Add(new SearchProperty(row));
            }
            return items;
        }

       

        /// <summary>
        /// Displays the name of the search property.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }
    }
}
